package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.WorkspaceMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private WorkspaceMapper workspaceMapper;

    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        Integer newUsers = workspaceMapper.getNewUsers(begin, end);
        if (newUsers == null) {
            newUsers = 0;
        }
        Double turnover = workspaceMapper.getTurnover(begin, end, Orders.COMPLETED);
        if (turnover == null) {
            turnover = 0.0;
        }
        Integer validOrderCount = workspaceMapper.getOrderCountByStatus(begin, end, Orders.COMPLETED);
        if (validOrderCount == null) {
            validOrderCount = 0;
        }
        Integer totalOrderCount = workspaceMapper.getTotalOrderCount(begin, end);
        if (totalOrderCount == null) {
            totalOrderCount = 0;
        }
        Double unitPrice = validOrderCount == 0 ? 0.0 : turnover / validOrderCount;
        Double orderCompletionRate = totalOrderCount == 0 ? 0.0 : validOrderCount.doubleValue() / totalOrderCount;
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .build();
    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Integer sold = workspaceMapper.countSetmealByStatus(StatusConstant.ENABLE);
        Integer discontinued = workspaceMapper.countSetmealByStatus(StatusConstant.DISABLE);
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public DishOverViewVO getDishOverView() {
        Integer sold = workspaceMapper.countDishByStatus(StatusConstant.ENABLE);
        Integer discontinued = workspaceMapper.countDishByStatus(StatusConstant.DISABLE);
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public OrderOverViewVO getOrderOverView() {
        LocalDate today = LocalDate.now();
        LocalDateTime begin = LocalDateTime.of(today, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(today, LocalTime.MAX);
        Integer allOrders = workspaceMapper.getTotalOrderCount(begin, end);
        Integer cancelledOrders = workspaceMapper.getOrderCountByStatus(begin, end, Orders.CANCELLED);
        Integer deliveredOrders = workspaceMapper.getOrderCountByStatus(begin, end, Orders.CONFIRMED);
        Integer waitingOrders = workspaceMapper.getOrderCountByStatus(begin, end, Orders.TO_BE_CONFIRMED);
        Integer completedOrders = workspaceMapper.getOrderCountByStatus(begin, end, Orders.COMPLETED);
        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .completedOrders(completedOrders)
                .build();
    }
}
